package com.variometer.activities;

/**
 * Created by michal.kudlacak on 8/1/13.
 */
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;

import com.activities.variometer.R;
import com.variometer.applicaton.Variometer;
import com.variometer.models.ActualData;
import com.variometer.observer.Observable;
import com.variometer.observer.Observer;

import java.io.File;
import java.util.Collection;
import java.util.List;

import org.mapsforge.android.maps.MapActivity;
import org.mapsforge.android.maps.MapView;
import org.mapsforge.android.maps.overlay.ArrayItemizedOverlay;
import org.mapsforge.android.maps.overlay.ArrayWayOverlay;
import org.mapsforge.android.maps.overlay.Overlay;
import org.mapsforge.android.maps.overlay.OverlayItem;
import org.mapsforge.android.maps.overlay.OverlayWay;
import org.mapsforge.android.maps.overlay.WayOverlay;
import org.mapsforge.core.GeoPoint;

public class OfflineMapActivity extends MapActivity implements LocationsDrawable,Observer{
    MapView mapView;
    int lastDrawLocation=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView = new MapView(this);
        mapView.setClickable(true);
        mapView.setBuiltInZoomControls(true);
        mapView.setMapFile(new File("/storage/sdcard0/DCIM/Maps/slovakia130617.map"));
        setContentView(mapView);

        ((Variometer)this.getApplication()).addActualDataObserver(this);



    }


    @Override
    public void DrawLocations(List<Location> locations) {

        Paint wayPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        wayPaint.setStyle(Paint.Style.STROKE);
        wayPaint.setColor(Color.BLUE);
        wayPaint.setAlpha(192);
        wayPaint.setStrokeWidth(6);

        ArrayWayOverlay wayOverlay = new ArrayWayOverlay (wayPaint,wayPaint);
        OverlayWay way = new OverlayWay();


        GeoPoint newPoints[][] = new GeoPoint[locations.size()][1];
        for (Location location:locations)
        {
            GeoPoint point = new GeoPoint(location.getLatitude(),location.getLongitude());

        }
        way.setWayNodes(newPoints);
        wayOverlay.addWay(way);
        mapView.getOverlays().add(wayOverlay);


        GeoPoint point = new GeoPoint(locations.get(locations.size()-1).getLatitude(),locations.get(locations.size()-1).getLongitude());
        Drawable paragliderMarker = getResources().getDrawable(R.drawable.paraglider_icon);
        ArrayItemizedOverlay overlay = new ArrayItemizedOverlay(paragliderMarker);
        OverlayItem item = new OverlayItem(point,"Item","Item");
        overlay.addItem(item);
        mapView.getOverlays().add(overlay);
    }

    @Override
    public void onChange(Observable model) {
        if (model instanceof ActualData)
        {
            List<Location> locations = ((ActualData)model).getLocations();
            List<Location> locationToDraw = null;

            if(lastDrawLocation == -1)
            {
                locationToDraw = locations;
            }
            else
            {
                for(int i=0;i<locations.size();i++)
                {
                    if(i>lastDrawLocation)
                    {
                        locationToDraw.add(locations.get(i));
                    }
                }
            }
            lastDrawLocation = locations.size();
            DrawLocations(locationToDraw);


        }
    }
}
